﻿package  {
	
	public class tentacle extends moveableobj {
		var tens:Array=new Array();
		var tar:displaycell=null;
		var fom:displaycell=null;
		var process:Number=0; //0 to 1
		var growtask:Number=1;//0 to 1
		var transferc:Array=new Array();
		//var pairtentacle:tentacle=null;
		static var tantaclewidth:Number=20;
		static var tantacleheight:Number=36;
		var initialed:Boolean=false;
		public function getinitialed(){return initialed;}
		public function tentacle() {
			// constructor code
		}
		public function gettarget():displaycell{return tar;}
		public function getfrom():displaycell{return fom;}
		public function init(t:displaycell,f:displaycell) {
			tar=t;
			fom=f;
			this.rotation=new vector2(t.x-f.x,t.y-f.y).getdegree();
			var tmpv2:vector2=f.getcenter();
			this.moveto(tmpv2.x+(f.width/2)*Math.cos(this.rotation*(Math.PI/180)),tmpv2.y+(f.width/2)*Math.sin(this.rotation*(Math.PI/180)));
			initialed=true;
			setprocess(0);
			
		}
		public function uninit()
		{
			var i:uint;
			if(initialed)
			{
			
			setprocess(0);
			initialed=false;
			for(i=0;i<transferc.length;i++)
			{
				fom.acell.betransfer(transferc[i].power,fom.getteam());
			}
			transferc=new Array();
			tar=null;
			fom=null;
			}
		}
		public function actuallenght():Number
		{
			if(initialed)
			{
				return tar.getcenter().sub(fom.getcenter()).length()-(tar.width/2+fom.width/2);
			}
			return 0;
		}
		public function setprocess(a:Number)// 0 to 1
		{
			if(initialed)
			{
			if(a>1)a=1;
			var lll:Number=actuallenght();
			var tarnum:uint=(lll*a)/tantaclewidth;
			var rppo:Number=(lll*a)-(tarnum*tantaclewidth);
			var tmpbj:tantacleunit;
			var i:uint;
			while(tens.length>tarnum)
			{
				tmpbj=tens.shift()
				this.removeChild(tmpbj);
			}
			while(tens.length<tarnum)
			{
				tens.push(new tantacleunit());
				this.addChild(tens[tens.length-1]);
				tens[tens.length-1].width=tantaclewidth;
				tens[tens.length-1].height=tantacleheight;
				tens[tens.length-1].gotoAndStop(fom.getteam()*2+1);
			}
			for(i=0;i<tens.length;i++)
			{
				tens[i].x=i*tantaclewidth+rppo
				tens[i].y=-tantacleheight/2;
			}
			process=a;
			}
		}
		public function growconstantspeed(sp:Number)
		{
			var lll:Number=tar.getcenter().sub(fom.getcenter()).length()-(tar.width/2+fom.width/2);
			grow(sp/lll);
		}
		public function grow(a:Number)
		{
			if(process>growtask-a && process<growtask+a)
			{
				setprocess(growtask);
			}
			if(process<growtask)
			{
			setprocess(process+a);
			}
			else if(process>growtask)
			{
			setprocess(process-a);
			}
		}
		public function setgrowtask(a:Number)
		{
			growtask=a;
		}
		public function addtransfer(sp:Number,atk:Number)
		{
			transferc.push(new transferpacket(sp,atk));
		}
		public function transfer()
		{
			if(initialed)
			{
			var i:uint;
			var iss:uint;
			var tt:uint=fom.getteam()*2+1;
			
			for(i=0;i<tens.length;i++)
			{
				tens[i].gotoAndStop(tt);
			}
			
			for(i=0;i<transferc.length;i++)
			{
				
				transferc[i].process+=transferc[i].speed;
				
				iss=transferc[i].process;
				
				if(iss<tens.length)
				{
					
					tens[iss].gotoAndStop(tt+1);
				}
			}
			for(i=0;i<transferc.length;)
			{
				if(transferc[i].process>=tens.length)
				{
					//trace("sl");
					if(process==growtask)
					{
					tar.betransfer(transferc[i].power,fom.getteam());
					}
					else
					{
						fom.betransfer(transferc[i].power,fom.getteam());
					}
					transferc.splice(i, 1);
				}
				else
				{
					i++;
				}
			}
			}
			
		}
		
		public function cutt(st:vector2,ed:vector2):Boolean
		{
			var vts:vector2=new vector2(0,0);
			vts.formdegree(this.rotation);
			vts=vts.mulf(actuallenght()*this.process);
			//trace("gt",process);
			vts=vts.add(new vector2(this.x,this.y));
			if(vector2.v2intersect(st,ed,new vector2(this.x,this.y),vts))
			{
				trace("hit");
				return true;
			}
			trace("not hit");
			return false;
		}
		
	}
	
}
